package com.houseman.workFlow.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.houseman.workFlow.bean.ApprovalInfo;
import com.houseman.workFlow.dao.ApproveDao;
import com.houseman.workFlow.utils.DBUtils;

public class ApproveDaoImpl implements ApproveDao{

	/**
	 * 创建流程审批信息
	 * @throws Exception
	 */
	public void saveApproveInfo(ApprovalInfo info) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		//insert workFlowGraph
		PreparedStatement ps = conn.prepareStatement("INSERT INTO APPROVALINFO(WORKID,STEP,OPINION,TRANSPOND,AGREE,APPROVER,APPROVENAME,APPDATE) VALUES(?,?,?,?,?,?,?,NOW())");
		ps.setInt(1, info.getWorkId());
		ps.setInt(2, info.getStep());
		ps.setString(3, info.getOpinion());
		ps.setInt(4, info.getTranspond());
		ps.setInt(5, info.getAgree());
		ps.setString(6, info.getApprover());
		ps.setString(7, info.getApproveName());
		
		ps.execute(); //新增 删除
		
		//关闭连接
		DBUtils.close(conn, ps);
	}
	
	/**
	 * 获取流程审批信息
	 * 审批时间排序(最新的在前)
	 * @param workId
	 * @return
	 * @throws Exception
	 */
	public List<ApprovalInfo> queryAppInfoByWorkId(int workId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT APPID,WORKID,STEP,OPINION,TRANSPOND,AGREE,APPROVER,APPROVENAME,APPDATE FROM APPROVALINFO WHERE WORKID=? ORDER BY APPDATE");
		ps.setInt(1, workId);
		
		ResultSet rs = ps.executeQuery();

		List<ApprovalInfo> info = new ArrayList<ApprovalInfo>();
		while (rs.next()) {
			ApprovalInfo ai = new ApprovalInfo();
			ai.setAppId(rs.getInt("APPID"));
			ai.setWorkId(rs.getInt("WORKID"));
			ai.setStep(rs.getInt("STEP"));
			ai.setOpinion(rs.getString("OPINION"));
			ai.setTranspond(rs.getInt("TRANSPOND"));
			ai.setAgree(rs.getInt("AGREE"));
			ai.setApprover(rs.getString("APPROVER"));
			ai.setApproveName(rs.getString("APPROVENAME"));
			ai.setAppData(rs.getString("APPDATE"));
			
			info.add(ai);
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return info;
	}

	/**
	 * 获取最新流程审批信息
	 * @param workId
	 * @return
	 * @throws Exception
	 */
	public ApprovalInfo queryNewestByWorkId(int workId) throws Exception {
		//获得连接
		Connection conn = DBUtils.geConnection();
		
		PreparedStatement ps = conn.prepareStatement("SELECT APPID,WORKID,STEP,OPINION,TRANSPOND,AGREE,APPROVER,APPROVENAME,APPDATE FROM APPROVALINFO WHERE WORKID=? ORDER BY APPDATE LIMIT 1");
		ps.setInt(1, workId);
		
		ResultSet rs = ps.executeQuery();

		ApprovalInfo ai = new ApprovalInfo();
		while (rs.next()) {
			ai.setAppId(rs.getInt("APPID"));
			ai.setWorkId(rs.getInt("WORKID"));
			ai.setStep(rs.getInt("STEP"));
			ai.setOpinion(rs.getString("OPINION"));
			ai.setTranspond(rs.getInt("TRANSPOND"));
			ai.setAgree(rs.getInt("AGREE"));
			ai.setApprover(rs.getString("APPROVER"));
			ai.setApproveName(rs.getString("APPROVENAME"));
			ai.setAppData(rs.getString("APPDATE"));
		}
		
		//关闭连接
		DBUtils.close(conn, ps, rs);
		return ai;
	}
}
